﻿using Entities;
using Mapster;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Utils;
using VO.Admin;

namespace Bridges
{
    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    public class Admin
    {
        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string List(string data)
        {
            ListVO vo = JsonConvert.DeserializeObject<ListVO>(data);
            int total = 0;
            List<UserEntity> users = SqlSugarHelper.Db.Queryable<UserEntity>()
                .Where(ue => ue.Id != 1) // 排除超级管理员
                .Where(ue => ue.IsActive == 1)
                .WhereIF(!string.IsNullOrEmpty(vo.Content), ue => ue.UserName.Contains(vo.Content) || ue.RealName.Contains(vo.Content))
                .OrderByDescending(ue => ue.Id)
                .ToPageList(vo.PageIndex, vo.PageSize, ref total);

            return JsonConvert.SerializeObject(new {
                StatusCode = 200,
                Succeeded = true,
                Data = users,
                Errors = "",
                Extras = new { Total = total },
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 增加用户
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        public string Save(string data)
        {
            SaveVO vo = JsonConvert.DeserializeObject<SaveVO>(data);
            // 判断用户是否存在
            bool any = SqlSugarHelper.Db.Queryable<UserEntity>()
                .Where(u => u.IsActive == 1)
                .Where(u => u.UserName == vo.UserName)
                .Any();
            if (any)
            {
                return JsonConvert.SerializeObject(new {
                    StatusCode = -1,
                    Succeeded = false,
                    Data = "",
                    Errors = "该用户已存在",
                    Extras = new { },
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }

            SqlSugarHelper.Db.Insertable<UserEntity>(vo.Adapt<UserEntity>()).ExecuteCommand();

            return JsonConvert.SerializeObject(new {
                StatusCode = 200,
                Succeeded = true,
                Data = vo,
                Errors = "",
                Extras = new { },
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        public string Delete(string data)
        {
            DeleteVO vo = JsonConvert.DeserializeObject<DeleteVO>(data);

            SqlSugarHelper.Db.Updateable<UserEntity>().SetColumns(u => u.IsActive == 0).Where(u => u.Id == vo.Id).Where(u => u.Id != 1).ExecuteCommand();

            return JsonConvert.SerializeObject(new {
                StatusCode = 200,
                Succeeded = true,
                Data = "",
                Errors = "",
                Extras = new { },
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        public string Update(string data)
        {
            UpdateVO vo = JsonConvert.DeserializeObject<UpdateVO>(data);
            // 判断用户是否存在
            bool any = SqlSugarHelper.Db.Queryable<UserEntity>()
                .Where(u => u.IsActive == 1)
                .Where(u => u.UserName == vo.UserName)
                .Where(u => u.Id != vo.Id)
                .Any();
            if (any)
            {
                return JsonConvert.SerializeObject(new {
                    StatusCode = -1,
                    Succeeded = false,
                    Data = "",
                    Errors = "该账户已存在",
                    Extras = new { },
                    Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
            }

            SqlSugarHelper.Db.Updateable<UserEntity>(vo).ExecuteCommand();

            return JsonConvert.SerializeObject(new {
                StatusCode = 200,
                Succeeded = true,
                Data = vo,
                Errors = "",
                Extras = new { },
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 查看用户
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        public string Show(string data)
        {
            ShowVO vo = JsonConvert.DeserializeObject<ShowVO>(data);

            UserEntity user = SqlSugarHelper.Db.Queryable<UserEntity>()
                .Where(u => u.IsActive == 1)
                .InSingle(vo.Id);

            return JsonConvert.SerializeObject(new {
                StatusCode = 200,
                Succeeded = true,
                Data = user,
                Errors = "",
                Extras = new { },
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }
    }
}
